package com.zmn.plat.admin.controller.orderconfig.returnvisit.question;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zmn.common.dto2.ResponseDTO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.utils.string.InitialUtil;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.plat.model.entity.orderconfig.returnvisitcontent.ReturnVisitQuestion;

/**
 * @author linfeng
 * @description 回访问题修改控制器
 * @since 2020/04/16 16:49
 * @modifier
 **/
@RestController
@RequestMapping("/orderconfig/returnvisit/question/")
public class ReturnVisitQuestionModifyController extends ReturnVisitQuestionBaseController {
    private Logger logger = LoggerFactory.getLogger(ReturnVisitQuestionModifyController.class);

    /**
     * 新增/修改
     *
     * @param returnVisitQuestion
     *            产品新增修改实体
     * @return ResultDTO 执行结果
     */
    @RequestMapping(value = "addEdit")
    @RequiresPermissions("/orderconfig/returnvisit/question/detail.action")
    public ResponseDTO addEdit(HttpServletRequest request, @RequestBody ReturnVisitQuestion returnVisitQuestion) {
        String operator = getMcStaffRealName();
        String lable = "";
        try {
            if (returnVisitQuestion.getQuestionId() == null) {
                lable = "新增问题";
                returnVisitQuestion.setCreater(operator);
                returnVisitQuestion.setCreateTime(new Date());
                returnVisitQuestion.setUpdater(operator);
                returnVisitQuestion.setUpdateTime(new Date());
                returnVisitQuestion.setStatus(GlobalConsts.YES);
                returnVisitQuestion.setFirstLetter(InitialUtil.getFristEnName(returnVisitQuestion.getQuestion()));
                returnVisitQuestionService.insert(returnVisitQuestion);
                return ResponseDTO.success(true,"新增成功！");
            } else {
                lable = "修改问题";
                returnVisitQuestion.setUpdater(operator);
                returnVisitQuestion.setUpdateTime(new Date());
                returnVisitQuestion.setFirstLetter(InitialUtil.getFristEnName(returnVisitQuestion.getQuestion()));
                returnVisitQuestionService.updateReturnVisitQuestionByKey(returnVisitQuestion);
                return ResponseDTO.success(true,"修改成功！");
            }
        } catch (org.springframework.dao.DuplicateKeyException ex) {
            logger.error("回访问题【{}】已经存在，不能重复添加！", returnVisitQuestion.getQuestion());
            return ResponseDTO.fail("回访问题已经存在，不能重复添加！");
        } catch (Exception ex) {
            logger.error(lable + "失败", ex);
            return ResponseDTO.fail(lable + "失败！");
        }
    }

    @RequestMapping(value = "updateStatus")
    @RequiresPermissions("/orderconfig/returnvisit/question/updateStatus.action")
    public ResponseDTO updateStatus(HttpServletRequest request, HttpServletResponse response,
        @RequestParam(value = "ids", defaultValue = "") String ids, @RequestParam(value = "status", defaultValue = "0") int status) {

        if (StringUtil.isBlank(ids)) {
            return ResponseDTO.fail("请选择要更改状态的数据！");
        }

        List<Integer> questionIds = StringUtil.convertStrsToList(ids);
        returnVisitQuestionService.updateStatusBatch(questionIds, status, getMcStaffRealName());
        return ResponseDTO.success(true,"操作成功！");
    }

    /**
     * 描述:修改排序
     *
     * @param id
     *            分类id
     * @param sort
     *            修改的状态
     * @return: ResultDTO 执行结果
     * @auther: tanbiao
     * @since:20191022
     */
    @RequestMapping(value = "updateSort")
    @RequiresPermissions("/orderconfig/returnvisit/question/list.action")
    public ResponseDTO updateSort(HttpServletRequest request, HttpServletResponse response,
        @RequestParam(value = "id", defaultValue = "0") Integer id, @RequestParam(value = "sort", defaultValue = "0") Integer sort) {

        if (id == 0) {
            return ResponseDTO.fail("请选择要更改排序的数据！");
        }
        if (sort == 0) {
            return ResponseDTO.fail("请设置排序值！");
        }
        returnVisitQuestionService.updateSortKey(id, sort, getMcStaffRealName());
        return ResponseDTO.success(true,"操作成功！");
    }

    /**
     * 描述:修改首字母
     *
     * @param id
     *            分类id
     * @param firstLetter
     *            首字母
     * @return: ResultDTO 执行结果
     * @auther: tanbiao
     * @since:20191022
     */
    @RequestMapping(value = "updateFirstLetter")
    @RequiresPermissions("/orderconfig/returnvisit/question/list.action")
    public ResponseDTO updateFirstLetter(HttpServletRequest request, HttpServletResponse response,
        @RequestParam(value = "id", defaultValue = "0") Integer id,
        @RequestParam(value = "firstLetter", defaultValue = "") String firstLetter) {

        if (id == 0) {
            return ResponseDTO.fail("请选择要更改首字母的数据！");
        }
        if (StringUtil.isBlank(firstLetter)) {
            return ResponseDTO.fail("请设置分类首字母！");
        }
        returnVisitQuestionService.updateFirstLetterByKey(id, firstLetter, getMcStaffRealName());
        return ResponseDTO.success(true,"操作成功！");
    }
}
